﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using CarlosAg.ExcelXmlWriter;

namespace BangkokSteel
{
    public partial class frmPurchaseOrderAddnew : Form, MainFormListener
    {
        private TabControl mainTab;
        private MySqlConnection conn = null;
        private MySqlDataAdapter adapter = null;
        private MySqlDataAdapter adapter2 = null;
        private string MODE = "";
        private string PO_Key2 = "";

        public frmPurchaseOrderAddnew(TabControl mainTab)
        {
            this.mainTab = mainTab;
            InitializeComponent();
        }

        #region Form event

        private void frmPurchaseOrderAddnew_Load(object sender, EventArgs e)
        {
            conn = new MySqlConnection(BangkokSteel.Properties.Settings.Default.bangkoksteelConnectionString);

            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            conn.Open();

            string sql = " SELECT id,po_id";
            sql += " ,(SELECT name FROM supplier_type WHERE ID = purchase_order.type limit 1) as type";
            sql += " ,(SELECT name FROM supplier WHERE supplier_id = purchase_order.supplier_id limit 1) as supplier_id";
            sql += " ,po_date";
            sql += " ,(SELECT name FROM payment_term WHERE ID = purchase_order.payment_term_id limit 1) as payment_term_id";
            sql += " ,(SELECT name FROM received_condition WHERE ID = purchase_order.received_condition_id limit 1) as received_condition_id";
            sql += " ,(SELECT name FROM vat WHERE ID = purchase_order.vat_id limit 1) as vat_id";
            sql += " ,remark";
            sql += " ,total";
            sql += " ,transportation_cost";
            sql += " FROM purchase_order;";

            adapter = new MySqlDataAdapter(sql, conn);
            adapter.Fill(dataSet1, "purchase_order");

            dataGridView1.DataSource = dataSet1.Tables["purchase_order"];
            SetCbo();
        }

        /// <tab purchase order>
        ///
        /// </tab purchase order>
        /// 

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.mainTab.TabPages.Remove(this.mainTab.SelectedTab);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            EnableOrDisableMainPage(false);

            MODE = "ADD";

            ClearFields();

            panel1.Visible = true;

            txtPoId.Focus();
            txtPoId.Select();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
            {
                EnableOrDisableMainPage(false);

                DataGridViewRow row = dataGridView1.SelectedRows[0];

                MODE = "EDIT";

                DataSet ds = new DataSet();
                string sql = string.Format("SELECT * FROM purchase_order where id = {0}", row.Cells["id"].Value.ToString());
                MySqlDataAdapter adap = new MySqlDataAdapter(sql, conn);
                adap.Fill(ds);

                if (!(ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0))
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    txtId.Text = dr["id"].ToString();
                    txtPoId.Text = dr["po_id"].ToString();
                    cboSupplier.SelectedValue = dr["supplier_id"].ToString();
                    cboSupplierType.SelectedValue = dr["type"].ToString();
                    dpPoDate.Text = dr["po_date"].ToString();
                    cboPaymentTerm.SelectedValue = dr["payment_term_id"].ToString();
                    cboReceiveCon.SelectedValue = dr["received_condition_id"].ToString();
                    cboVat.SelectedValue = dr["vat_id"].ToString();
                    txtRemark.Text = dr["remark"].ToString();
                    txtTotal.Text = dr["total"].ToString();
                    txtTransCost.Text = dr["transportation_cost"].ToString();
                }

                panel1.Visible = true;

                txtPoId.Focus();
                txtPoId.SelectAll();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
            {
                if (DialogResult.Yes == MessageBox.Show("คุณต้องการลบข้อมูลนี้ใช่หรือไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    DataGridViewRow row = dataGridView1.SelectedRows[0];

                    string sql = "DELETE FROM `purchase_order` WHERE (`id` = @Original_id)";

                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@Original_id", MySqlDbType.String).Value = row.Cells["id"].Value;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว");
                    MODE = "";

                    // Refresh for New Data
                    dataSet1.Tables["purchase_order"].Clear();
                    adapter.Fill(dataSet1, "purchase_order");
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (Validation())
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }

                    conn.Open();

                    MySqlCommandBuilder builder = new MySqlCommandBuilder(adapter);

                    if ("ADD".Equals(MODE))
                    {
                        MySqlCommand cmd = (MySqlCommand)builder.GetInsertCommand(true);

                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("@po_id", MySqlDbType.VarChar).Value = txtPoId.Text;
                        cmd.Parameters.Add("@type", MySqlDbType.Int32).Value = cboSupplierType.SelectedValue;
                        cmd.Parameters.Add("@supplier_id", MySqlDbType.VarChar).Value = cboSupplier.SelectedValue;
                        cmd.Parameters.Add("@po_date", MySqlDbType.Date).Value = dpPoDate.Text;
                        cmd.Parameters.Add("@payment_term_id", MySqlDbType.Int32).Value = cboPaymentTerm.SelectedValue;
                        cmd.Parameters.Add("@received_condition_id", MySqlDbType.Int32).Value = cboReceiveCon.SelectedValue;
                        cmd.Parameters.Add("@vat_id", MySqlDbType.Int32).Value = cboVat.SelectedValue;
                        cmd.Parameters.Add("@remark", MySqlDbType.VarChar).Value = txtRemark.Text;
                        cmd.Parameters.Add("@total", MySqlDbType.Double).Value = txtTotal.Text;
                        cmd.Parameters.Add("@transportation_cost", MySqlDbType.Double).Value = txtTransCost.Text;

                        int affectedRow = cmd.ExecuteNonQuery();

                        MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
                        ClearFields();

                        txtId.Focus();
                        txtId.SelectAll();

                    }
                    else if ("EDIT".Equals(MODE))
                    {
                        string sql = @"UPDATE `purchase_order` SET `po_id` = @po_id, `supplier_id` = @supplier_id ";
                        sql = sql + " , `type` = @type , `po_date` = @po_date , `payment_term_id` = @payment_term_id";
                        sql = sql + " , `received_condition_id` = @received_condition_id , `vat_id` = @vat_id";
                        sql = sql + " , `remark` = @remark , `total` = @total , `transportation_cost` = @transportation_cost";
                        sql = sql + " WHERE (`id` = @Original_id)";

                        MySqlCommand cmd = new MySqlCommand(sql, conn);
                        cmd.Parameters.Add("@po_id", MySqlDbType.VarChar).Value = txtPoId.Text;
                        cmd.Parameters.Add("@type", MySqlDbType.Int32).Value = cboSupplierType.SelectedValue;
                        cmd.Parameters.Add("@supplier_id", MySqlDbType.VarChar).Value = cboSupplier.SelectedValue;
                        cmd.Parameters.Add("@po_date", MySqlDbType.Date).Value = dpPoDate.Text;
                        cmd.Parameters.Add("@payment_term_id", MySqlDbType.Int32).Value = cboPaymentTerm.SelectedValue;
                        cmd.Parameters.Add("@received_condition_id", MySqlDbType.Int32).Value = cboReceiveCon.SelectedValue;
                        cmd.Parameters.Add("@vat_id", MySqlDbType.Int32).Value = cboVat.SelectedValue;
                        cmd.Parameters.Add("@remark", MySqlDbType.VarChar).Value = txtRemark.Text;
                        cmd.Parameters.Add("@total", MySqlDbType.Double).Value = txtTotal.Text;
                        cmd.Parameters.Add("@transportation_cost", MySqlDbType.Double).Value = txtTransCost.Text;
                        cmd.Parameters.Add("@Original_id", MySqlDbType.Int32).Value = txtId.Text;

                        cmd.ExecuteNonQuery();
                        
                        MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");

                        panel1.Visible = false;
                        EnableOrDisableMainPage(true);
                        ClearFields();

                        MODE = "";
                    }
                    // Refresh for New Data
                    dataSet1.Tables["purchase_order"].Clear();
                    adapter.Fill(dataSet1, "purchase_order");
                }
            }
            catch (FormatException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EnableOrDisableMainPage(true);

            panel1.Visible = false;
            MODE = "";
            ClearFields();
        }

        private void tb_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = false;
            EnableOrDisableMainPage(true);
            EnableOrDisableMainPage2(true);
            switch (tb.SelectedIndex)
            {
                default:
                    break;
                case 0:
                    {
                        
                    }
                    break;
                case 1:
                    {

                        if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
                        {
                            DataGridViewRow row = dataGridView1.SelectedRows[0];

                            loadControlTab2(row);

                            string sql = " SELECT id";
                            sql += " ,(SELECT po_id FROM purchase_order WHERE ID = purchase_order_detail.purchase_order_id limit 1) as purchase_order_id";
                            sql += " ,(SELECT name FROM product WHERE ID = purchase_order_detail.product_id limit 1) as product_id";
                            sql += " ,quantity";
                            sql += " ,(SELECT name FROM unit WHERE ID = purchase_order_detail.unit_id limit 1) as unit_id";
                            sql += " ,price";
                            sql += " ,discount";
                            sql += " FROM purchase_order_detail";
                            sql += string.Format(" where purchase_order_id = {0};",row.Cells["id"].Value.ToString());

                            adapter2 = new MySqlDataAdapter(sql, conn);
                            adapter2.Fill(dataSet1, "purchase_order_detail");
                            dataGridView2.DataSource = dataSet1.Tables["purchase_order_detail"];


                            dataSet1.Tables["purchase_order_detail"].Clear();
                            adapter2.Fill(dataSet1, "purchase_order_detail");
                            this.dataGridView2.Columns["id"].Visible = false;

                            PO_Key2 = row.Cells["id"].Value.ToString();
                        }
                        else
                        {
                            MessageBox.Show("กรุณาเลือกข้อมูล");
                        }
                    }
                    break;
            }

        }

        /// <tab purchase order detail>
        /// 
        /// </tab purchase order detail>

        private void btnNew2_Click(object sender, EventArgs e)
        {
            EnableOrDisableMainPage2(false);

            MODE = "ADD";

            ClearFields();

            txtPoId2.Text = lblPoId.Text;
            panel2.Visible = true;

            cboProduct.Focus();
        }

        private void btnEdit2_Click(object sender, EventArgs e)
        {
            if (dataGridView2.Rows.Count > 0 && dataGridView2.SelectedRows.Count > 0)
            {
                EnableOrDisableMainPage2(false);

                DataGridViewRow row = dataGridView2.SelectedRows[0];

                MODE = "EDIT";

                DataSet ds = new DataSet();
                string sql = string.Format("SELECT * FROM purchase_order_detail where id = {0}", row.Cells["id"].Value.ToString());
                MySqlDataAdapter adap = new MySqlDataAdapter(sql, conn);
                adap.Fill(ds);

                if (!(ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0))
                {
                    DataRow dr = ds.Tables[0].Rows[0];

                    txtId2.Text = dr["id"].ToString();
                    txtPoId2.Text = lblPoId.Text;
                    cboProduct.SelectedValue = dr["product_id"].ToString();
                    txtQuantity.Text = dr["quantity"].ToString();
                    cboUnit.SelectedValue = dr["unit_id"].ToString();
                    txtPrice.Text = dr["price"].ToString();
                    txtDiscount.Text = dr["discount"].ToString();
                }

                panel2.Visible = true;

                cboProduct.Focus();
            }
        }

        private void btnDelete2_Click(object sender, EventArgs e)
        {

        }

        private void btnOk2_Click(object sender, EventArgs e)
        {
            try
            {
                if (Validation2())
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }

                    conn.Open();

                    if ("ADD".Equals(MODE))
                    {
                        string sql = "INSERT INTO `purchase_order_detail` (`purchase_order_id`, `product_id`, `quantity`, `unit_id`, `price`, `discount`) ";
                        sql += " VALUES (@purchase_order_id, @product_id, @quantity, @unit_id, @price, @discount)";

                        MySqlCommand cmd = new MySqlCommand(sql, conn);
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("@purchase_order_id", MySqlDbType.Int32).Value = PO_Key2;
                        cmd.Parameters.Add("@product_id", MySqlDbType.VarChar).Value = cboProduct.SelectedValue;
                        cmd.Parameters.Add("@quantity", MySqlDbType.Double).Value = txtQuantity.Text;
                        cmd.Parameters.Add("@unit_id", MySqlDbType.Int32).Value = cboUnit.SelectedValue;
                        cmd.Parameters.Add("@price", MySqlDbType.Double).Value = txtPrice.Text;
                        cmd.Parameters.Add("@discount", MySqlDbType.Double).Value = txtDiscount.Text;

                        int affectedRow = cmd.ExecuteNonQuery();

                        MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
                        ClearFields();

                        txtId.Focus();
                        txtId.SelectAll();

                    }
                    else if ("EDIT".Equals(MODE))
                    {
                        string sql = @"UPDATE `purchase_order_detail` SET `purchase_order_id` = @purchase_order_id, `product_id` = @product_id ";
                        sql = sql + " , `quantity` = @quantity , `unit_id` = @unit_id ";
                        sql = sql + " , `price` = @price , `discount` = @discount ";
                        sql = sql + " WHERE (`id` = @Original_id2) ";

                        MySqlCommand cmd = new MySqlCommand(sql, conn);
                        cmd.Parameters.Add("@purchase_order_id", MySqlDbType.Int32).Value = PO_Key2;
                        cmd.Parameters.Add("@product_id", MySqlDbType.VarChar).Value = cboProduct.SelectedValue;
                        cmd.Parameters.Add("@quantity", MySqlDbType.Double).Value = txtQuantity.Text;
                        cmd.Parameters.Add("@unit_id", MySqlDbType.Int32).Value = cboUnit.SelectedValue;
                        cmd.Parameters.Add("@price", MySqlDbType.Double).Value = txtPrice.Text;
                        cmd.Parameters.Add("@discount", MySqlDbType.Double).Value = txtDiscount.Text;
                        cmd.Parameters.Add("@Original_id2", MySqlDbType.Int32).Value = txtId2.Text;

                        cmd.ExecuteNonQuery();

                        MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");

                        panel2.Visible = false;
                        EnableOrDisableMainPage(true);
                        ClearFields();

                        
                    }
                    MODE = "";
                    // Refresh for New Data
                    dataSet1.Tables["purchase_order_detail"].Clear();
                    adapter2.Fill(dataSet1, "purchase_order_detail");
                }
            }
            catch (FormatException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel2_Click(object sender, EventArgs e)
        {
            EnableOrDisableMainPage2(true);

            panel2.Visible = false;
            MODE = "";
            ClearFields();
        }

        private void cboProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboProduct.Items.Count > 0)
            {
                DataSet ds = new DataSet();
                MySqlDataAdapter adap = new MySqlDataAdapter("SELECT * FROM unit WHERE id in (SELECT unit_id FROM product_unit WHERE product_id = '" + cboProduct.SelectedValue + "') ", conn);
                adap.Fill(ds, "unit");
                cboUnit.DataSource = ds.Tables["unit"];
                cboUnit.DisplayMember = "name";
                cboUnit.ValueMember = "id";
            }
        }

        #endregion

        #region Function

        private void EnableOrDisableMainPage(bool isEnable)
        {
            btnNew.Enabled = isEnable;
            btnEdit.Enabled = isEnable;
            btnDelete.Enabled = isEnable;
            btnClose.Enabled = isEnable;
            dataGridView1.Enabled = isEnable;
        }

        private void EnableOrDisableMainPage2(bool isEnable)
        {
            btnNew2.Enabled = isEnable;
            btnEdit2.Enabled = isEnable;
            btnDelete2.Enabled = isEnable;
            btnClose.Enabled = isEnable;
            dataGridView2.Enabled = isEnable;
        }

        private void ClearFields()
        {
            //Panel1
            txtPoId.Text = "";
            txtId.Text = "";
            txtRemark.Text = "";
            txtTotal.Text = "";
            txtTransCost.Text = "";

            //Panel2
            txtQuantity.Text = "";
            txtPrice.Text = "";
            txtDiscount.Text = "";
        }

        private void SetCbo()
        {
            DataSet ds = new DataSet();

            //Supplier Type
            MySqlDataAdapter adapter2 = new MySqlDataAdapter("SELECT * FROM supplier_type", conn);
            adapter2.Fill(ds, "supplier_type");
            cboSupplierType.DataSource = ds.Tables["supplier_type"];
            cboSupplierType.DisplayMember = "name";
            cboSupplierType.ValueMember = "id";

            //Payment Term
            MySqlDataAdapter adapter3 = new MySqlDataAdapter("SELECT * FROM payment_term", conn);
            adapter3.Fill(ds, "payment_term");
            cboPaymentTerm.DataSource = ds.Tables["payment_term"];
            cboPaymentTerm.DisplayMember = "name";
            cboPaymentTerm.ValueMember = "id";

            //Receive Condition
            MySqlDataAdapter adapter4 = new MySqlDataAdapter("SELECT * FROM received_condition", conn);
            adapter4.Fill(ds, "received_condition");
            cboReceiveCon.DataSource = ds.Tables["received_condition"];
            cboReceiveCon.DisplayMember = "name";
            cboReceiveCon.ValueMember = "id";

            //VAT
            MySqlDataAdapter adapter5 = new MySqlDataAdapter("SELECT * FROM vat", conn);
            adapter5.Fill(ds, "vat");
            cboVat.DataSource = ds.Tables["vat"];
            cboVat.DisplayMember = "name";
            cboVat.ValueMember = "id";

            //Supplier
            MySqlDataAdapter adapter6 = new MySqlDataAdapter("SELECT * FROM supplier", conn);
            adapter6.Fill(ds, "supplier");
            cboSupplier.DataSource = ds.Tables["supplier"];
            cboSupplier.DisplayMember = "name";
            cboSupplier.ValueMember = "supplier_id";

            //Product
            MySqlDataAdapter adapter7 = new MySqlDataAdapter("SELECT * FROM product", conn);
            adapter7.Fill(ds, "product");
            cboProduct.DataSource = ds.Tables["product"];
            cboProduct.DisplayMember = "name";
            cboProduct.ValueMember = "id";

            //Product unit
            if (cboProduct.Items.Count > 0)
            {
                MySqlDataAdapter adapter8 = new MySqlDataAdapter("SELECT * FROM unit WHERE id in (SELECT unit_id FROM product_unit WHERE product_id = '" + cboProduct.SelectedValue + "') ", conn);
                adapter8.Fill(ds, "unit");
                cboUnit.DataSource = ds.Tables["unit"];
                cboUnit.DisplayMember = "name";
                cboUnit.ValueMember = "id";
            }
        }

        private void loadControlTab2(DataGridViewRow row)
        {
            //lblId.Text = row.Cells["id"].Value.ToString();
            lblPoId.Text = row.Cells["po_id"].Value.ToString();
            lblSupplier.Text = row.Cells["supplier_id"].Value.ToString();
            lblSupplierType.Text = row.Cells["type"].Value.ToString();
            lblPoDate.Text = row.Cells["po_date"].Value.ToString();
            lblPaymentTerm.Text = row.Cells["payment_term_id"].Value.ToString();
            lblReceiveCon.Text = row.Cells["received_condition_id"].Value.ToString();
            lblVat.Text = row.Cells["vat_id"].Value.ToString();
            lblRemark.Text = row.Cells["remark"].Value.ToString();
            lblTotal.Text = row.Cells["total"].Value.ToString();
            lblTransCost.Text = row.Cells["transportation_cost"].Value.ToString();
        }
        
        private bool Validation()
        {
            if (txtPoId.Text.Trim().Length == 0)
            {
                MessageBox.Show("กรุณาเลือกใส่หมายเลข PO ให้เรียบร้อย");

                txtPoId.Focus();
                txtPoId.SelectAll();

                return false;
            }

            if (cboSupplier.SelectedValue == null)
            {
                MessageBox.Show("กรุณาเลือก Supplier ให้เรียบร้อย");
                return false;
            }

            if (cboSupplierType.SelectedValue == null)
            {
                MessageBox.Show("กรุณาเลือก Supplier Type ให้เรียบร้อย");
                return false;
            }

            if (dpPoDate.Text.Trim().Length == 0)
            {
                MessageBox.Show("กรุณาเลือกวันที่ให้เรียบร้อย");

                dpPoDate.Focus();
                return false;
            }

            if (cboPaymentTerm.SelectedValue == null)
            {
                MessageBox.Show("กรุณาเลือกกลุ่มการจ่ายให้เรียบร้อย");
                return false;
            }

            if (cboReceiveCon.SelectedValue == null)
            {
                MessageBox.Show("กรุณาเลือกกลุ่มเงื่อนไขการรับสินค้าให้เรียบร้อย");
                return false;
            }

            if (cboVat.SelectedValue == null)
            {
                MessageBox.Show("กรุณาเลือก VAT ให้เรียบร้อย");
                return false;
            }

            if (txtRemark.Text.Trim().Length == 0)
            {
                MessageBox.Show("กรุณาเลือกใส่ Remark ให้เรียบร้อย");

                txtRemark.Focus();
                txtRemark.SelectAll();

                return false;
            }

            if (txtTotal.Text.Trim().Length == 0)
            {
                MessageBox.Show("กรุณาเลือกใส่ราคารวมให้เรียบร้อย");

                txtTotal.Focus();
                txtTotal.SelectAll();

                return false;
            }

            if (txtTransCost.Text.Trim().Length == 0)
            {
                MessageBox.Show("กรุณาเลือกใส่ค่าขนส่งให้เรียบร้อย");

                txtTransCost.Focus();
                txtTransCost.SelectAll();

                return false;
            }

            //=========================================================================================================
            DataSet ds = new DataSet();
            conn = new MySqlConnection(BangkokSteel.Properties.Settings.Default.bangkoksteelConnectionString);

            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            conn.Open();

            string sql = string.Format("SELECT * FROM purchase_order WHERE po_id = '{0}'", txtPoId.Text);
            MySqlDataAdapter adap = new MySqlDataAdapter(sql, conn);
            adap.Fill(ds);

            conn.Close();

            DataView dv = ds.Tables[0].DefaultView;

            if ("EDIT".Equals(MODE))
            {
                dv.RowFilter = string.Format("id <> {0}", txtId.Text);
            }

            if (!(dv.ToTable() == null || dv.ToTable().Rows.Count == 0))
            {
                MessageBox.Show("มีรหัสนี้อยู่ในระบบเรียบร้อย");
                return false;
            }
            //=========================================================================================================

            return true;
        }

        private bool Validation2()
        {
            if (cboProduct.SelectedValue == null)
            {
                MessageBox.Show("กรุณาเลือกสินค้าให้เรียบร้อย");
                return false;
            }

            if (txtQuantity.Text.Trim().Length == 0)
            {
                MessageBox.Show("กรุณาเลือกใส่จำนวนให้เรียบร้อย");

                txtQuantity.Focus();
                txtQuantity.SelectAll();

                return false;
            }

            if (cboUnit.SelectedValue == null)
            {
                MessageBox.Show("กรุณาเลือกหน่วยของสินค้าให้เรียบร้อย");
                return false;
            }

            if (txtPrice.Text.Trim().Length == 0)
            {
                MessageBox.Show("กรุณาเลือกใส่ราคาให้เรียบร้อย");

                txtPrice.Focus();
                txtPrice.SelectAll();

                return false;
            }

            if (txtDiscount.Text.Trim().Length == 0)
            {
                MessageBox.Show("กรุณาเลือกส่วนลดให้เรียบร้อย");

                txtDiscount.Focus();
                txtDiscount.SelectAll();

                return false;
            }

            return true;
        }

        #endregion

        #region Interface

        void MainFormListener.onMainFormClose()
        {
            // TODO
        }

        public void onTabPageAdded()
        {
            // TODO
        }

        #endregion        

        private void frmPurchaseOrderAddnew_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    {
                        switch (tb.SelectedIndex)
                        {
                            default:
                                break;
                            case 0:
                                {
                                    if (panel1.Visible)
                                    {
                                        btnCancel_Click(null, null);
                                    }
                                    else
                                    {
                                        btnClose_Click(null, null);
                                    }
                                }
                                break;
                            case 1:
                                {
                                    if (panel2.Visible)
                                    {
                                        btnCancel2_Click(null, null);
                                    }
                                    else
                                    {
                                        btnClose_Click(null, null);
                                    }
                                }
                                break;
                         }
                    }
                    break;
                case Keys.F2:
                    {
                        switch (tb.SelectedIndex)
                        {
                            default:
                                break;
                            case 0:
                                {
                                    if (panel1.Visible)
                                    {
                                        btnOk_Click(null, null);
                                    }
                                    else
                                    {
                                        btnNew_Click(null, null);
                                    }
                                }
                                break;
                            case 1:
                                {
                                    if (panel2.Visible)
                                    {
                                        btnOk2_Click(null, null);
                                    }
                                    else
                                    {
                                        btnNew2_Click(null, null);
                                    }
                                }

                                break;
                        }
                    }
                    break;
                case Keys.F3:
                    {
                        switch (tb.SelectedIndex)
                        {
                            default:
                                break;
                            case 0:
                                {
                                    btnEdit_Click(null, null);
                                }
                                break;
                            case 1:
                                {
                                    btnEdit2_Click(null, null);
                                }
                                break;
                        }
                    }
                    break;
                case Keys.F4:
                    {
                        btnDelete_Click(null, null);
                    }
                    break;
                case Keys.F5:
                    {
                        if (tb.SelectedIndex == 0)
                        {
                            tb.SelectedIndex = 1;
                        }
                        else
                        {
                            tb.SelectedIndex = 0;                        
                        }
                        tb_Click(null, null);
                    }
                    break;
            }
        
        }
    }
}
